package com.demo.simpletestapp

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager

/**
 * 获取App信息，工具类
 */
object AppManager {
    /** 获取已安装应用的信息*/
    fun getInstalledApps(context: Context): List<ApplicationInfo> {
        // 获取 PackageManager 实例
        val packageManager = context.packageManager
        // 获取所有已安装的应用程序信息
        return packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
    }

    /**遍历打印已安装应用的名称和包名*/
    fun printInstalledApps(context: Context) {
        val installedApps = getInstalledApps(context)
        for (appInfo in installedApps) {
            val appName = appInfo.loadLabel(context.packageManager).toString()
            val packageName = appInfo.packageName
            println("应用名称：$appName, 包名：$packageName")
        }
    }
}
